package com.nip.irregularvebsdict.adpters;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.nip.irregularvebsdict.R;
import com.nip.irregularvebsdict.items.IrregularVerbItem;

public class IrregularVerbsAdapter extends ArrayAdapter<IrregularVerbItem> {

	private List<IrregularVerbItem> sources;
	private List<IrregularVerbItem> allItem;
	private List<IrregularVerbItem> items;
	private LayoutInflater inflater;

	public IrregularVerbsAdapter(Context context, int resource,
			List<IrregularVerbItem> objects) {
		super(context, 1, objects);
		this.sources = objects;
		this.allItem = new ArrayList<IrregularVerbItem>(objects);
		items = new ArrayList<IrregularVerbItem>();
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (sources == null) {
			return 0;
		} else {
			return sources.size();
		}
	}

	@Override
	public IrregularVerbItem getItem(int position) {
		if (sources == null || sources.size() == 0) {
			return null;
		} else {
			return sources.get(position);
		}
	}

	public void update(List<IrregularVerbItem> objects) {
		this.sources = objects;
		this.allItem = new ArrayList<IrregularVerbItem>(objects);
		this.notifyDataSetChanged();
	}

	private View row;
	private ViewHolder holder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		row = convertView;

		if (convertView == null) {
			row = inflater.inflate(R.layout.irregular_custom_item_listview,
					null);
			holder = new ViewHolder();
			holder.tvBase = (TextView) row.findViewById(R.id.tvBase);
			holder.tvPassSimple = (TextView) row
					.findViewById(R.id.tvPassSimple);
			holder.tvPassPracitice = (TextView) row
					.findViewById(R.id.tvPassPractice);
			row.setTag(holder);
		} else {
			holder = (ViewHolder) row.getTag();
		}

		if (sources != null) {
			holder.tvBase.setText(sources.get(position).getBase());
			holder.tvPassSimple.setText(sources.get(position).getPastsimple());
			holder.tvPassPracitice.setText(sources.get(position)
					.getPastparticiple());
		}

		return row;
	}

	static class ViewHolder {
		public TextView tvBase;
		public TextView tvPassSimple;
		public TextView tvPassPracitice;
	}

	@Override
	public void clear() {
		sources.clear();
		allItem.clear();
		items.clear();
		super.clear();
	}
	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return filter;
	}

	private Filter filter = new Filter() {
		private FilterResults results;
		private List<IrregularVerbItem> itemsTemp;
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			if (constraint != null) {

				items.clear();
				
				for(IrregularVerbItem item : allItem)
				{
					if(item.getBase().toLowerCase().startsWith(constraint.toString().toLowerCase()))
					{
						items.add(item);
					}
				}
				
				results = new FilterResults();
				results.values = items;
				results.count = items.size();
				itemsTemp = new ArrayList<IrregularVerbItem>();
				return results;
			} else {
				return new FilterResults();
			}
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			itemsTemp.clear();
			itemsTemp = (List<IrregularVerbItem>) results.values;
			sources.clear();
			if(itemsTemp != null && itemsTemp.size() > 0)
			{
				for (IrregularVerbItem item	 : itemsTemp) {
					sources.add(item);
				}
				notifyDataSetChanged();
			}else if(itemsTemp == null)
			{
				for (IrregularVerbItem item	 : allItem) {
					sources.add(item);
				}
				notifyDataSetChanged();
			}
			notifyDataSetInvalidated();
			itemsTemp.clear();
		}

	};
}
